<?php

class grade_controller {
	
	private $params = NULL;
	
	function __construct($params) {
		$this->set_params($params);
		$this->init_access();
	}
	
	function __destruct() {
		
	}
	
	function set_params($params) {
		$this->params = $params;
	}
	
	private function init_access() {
		global $authentication;
		$access = ACCESS_GROUP_ADMIN | 
			ACCESS_GROUP_DIRECTOR_OF_STUDIES | 
			ACCESS_GROUP_ASSISTANT | 
			ACCESS_GROUP_EDUCATOR | 
			ACCESS_GROUP_SECRETARY_OF_THE_DEPARTMENT;
		if (! $authentication->have_access($access)) {
			$authentication->access_denied();
		}
	}
	
	/**
	 * Получает классы, которые доступны пользователю
	 */
	private function get_classes() {
		global $CFG;
		include_once($CFG->app_dir . '/class/class.classes.php');
		
		global $authentication;
		if ($authentication->have_access(ACCESS_GROUP_ADMIN)) {
			$classes = new classes('all', true);
		} else if ($authentication->have_access(ACCESS_GROUP_DIRECTOR_OF_STUDIES)) {
			$classes = new classes('all', true);
		} else if ($authentication->have_access(ACCESS_GROUP_ASSISTANT)) {
			$user_data = $authentication->get_user_data();
			$classes = new classes($user_data['streem'], true);
		} else if ($authentication->have_access(ACCESS_GROUP_EDUCATOR)) {
			$user_data = $authentication->get_user_data();
			$classes = new classes($user_data['class'], true);
		} else if ($authentication->have_access(ACCESS_GROUP_SECRETARY_OF_THE_DEPARTMENT)) {
			$classes = new classes('all', true);
		} else {
			$authentication->access_denied();
		}
		return $classes->get_classes();
	}
	
	/**
	 * Получает предметы, которые доступны пользователю
	 */
	private function get_subjects() {
		global $CFG;
		include_once($CFG->app_dir . '/department/class.departments.php');
		
		global $authentication;
		if (
			$authentication->have_access(
				ACCESS_GROUP_ADMIN | 
				ACCESS_GROUP_DIRECTOR_OF_STUDIES | 
				ACCESS_GROUP_ASSISTANT | 
				ACCESS_GROUP_EDUCATOR
			) 
		) {
			$departments = new departments();
		} else if ($authentication->have_access(ACCESS_GROUP_SECRETARY_OF_THE_DEPARTMENT)) {
			$user_data = $authentication->get_user_data();
			$departments = new departments($user_data['departments']);
		} else {
			$authentication->access_denied();
		}
		return $departments->get_subjects();
	}
	
	function action_show() {
		global $CFG;
		
		include_once($CFG->app_dir . '/class/class.classes.php');
		include_once($CFG->lib_dir . '/views/class.view.php');
		
		$view = new view(
			__DIR__ . '/views/grades_show.php', 
			array(
				'classes' => $this->get_classes(),
				'subjects' => $this->get_subjects()
			)
		);
		$view->render();
	}
	
	function action_report() {
		global $CFG;
		include_once($CFG->app_dir . '/grade/class.grades_report.php');
		include_once($CFG->lib_dir . '/views/class.view.php');
		
		$class = (! empty($_POST["c"])) ? $_POST["c"] : '10-1';
		$report_type = (! empty($_POST["m"])) ? $_POST["m"] : '09';
		$course_code = (! empty($_POST["s"])) ? $_POST["s"] : NULL;
		
		$grades_report = new grades_report($class, $report_type, $course_code);
		
		$view = new view(
			__DIR__ . '/views/grades_report.php', 
			array(
				'report_type' => $grades_report->get_report_type($report_type),
				'info' => $grades_report->get_report()
			)
		);
		$view->render_partial();
	}
}

?>
